package com.pureut.storage.service.impl;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.storage.domain.*;
import com.pureut.storage.domain.system.domain.SysDocument;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.PickingCompleteExport;
import com.pureut.storage.mapper.*;
import com.pureut.storage.service.*;
import com.pureut.storage.utils.DocumentTypeEnum;
import com.pureut.storage.utils.GenerationMethodEnum;
import com.pureut.storage.vo.PickingCompleteDetailVo;
import com.pureut.storage.vo.PickingCompleteVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateDocVo;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.QualityFeginService;
import com.pureut.system.api.RemoteUserService;
import com.pureut.system.api.SystemDocumentFeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.api.vo.MaterialNumberAndBatchNoVo;
import com.pureut.system.api.vo.QualityInspectionDocumentVo;
import com.pureut.system.api.vo.QualitySchemeConfigurationVo;
import com.pureut.system.api.vo.UpdateQualityStatusVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2022/12/29  15:55
 */
@Service
public class PickingCompleteServiceImpl extends ServiceImpl<PickingCompleteMapper, PickingComplete>  implements PickingCompleteService {

    @Resource
    private PickingCompleteMapper pickingCompleteMapper;

    @Resource
    private ProductionPickingService productionPickingService;

    @Resource
    private PickingCompleteDetailMapper pickingCompleteDetailMapper;

    @Resource
    private PickingCompleteDetailService pickingCompleteDetailService;

    @Resource
    FeignService feignService;

    @Resource
    WarehouseMapper warehouseMapper;

    //单据配置
    @Resource
    private SystemDocumentFeignService systemDocumentFeignService;

    //库存
    @Resource
    WmsStockInfoMapper wmsStockInfoMapper;

    @Resource
    WmsStockInfoService wmsStockInfoService;

    //公共物料
    @Resource
    PublicGoodsMapper publicGoodsMapper;

    //用户信息
    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private OutboundLogService outboundLogService;

    @Resource
    private OutboundLogMapper outboundLogMapper;

    @Resource
    private QualityFeginService qualityFeginService;


    /**
     * 新增返工出库单据
     * @param pickingComPleteVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addPickingComplete(PickingCompleteVo pickingComPleteVo) throws Exception {
        //取出返工出库单列表
        List<PickingCompleteDetailVo> list = pickingComPleteVo.getList();
        if(list.size()==0){
            throw new GlobalException("请至少添加一条物料明细");
        }

        int insert = 0;


        //定义物料id
        Long productId=0L;

        //定义物料编码
        String materialCode="";


        String sn = feignService.getAuthorityCoding("warehouse:outbound:reworksto:list");
        if (sn == null) {
            throw new GlobalException("该单据未进行配置,无法生成单据编码");
        }


        for (PickingCompleteDetailVo pickingCompleteDetailVo : list) {

            //新增返工出库单明细
            PickingCompleteDetail pickingCompleteDetail=new PickingCompleteDetail();

            //获取物料id
            productId = pickingCompleteDetailVo.getProductId();

            materialCode=pickingCompleteDetailVo.getItemCode();

            //添加批次号
            pickingCompleteDetail.setBatchNumber(pickingCompleteDetailVo.getBatchNumber());
            //添加产品号码
            pickingCompleteDetail.setPdoductNumber(pickingCompleteDetailVo.getPdoductNumber());
            //添加产品id
            pickingCompleteDetail.setProductId(pickingCompleteDetailVo.getProductId());
            //添加状态为待入库
            pickingCompleteDetail.setWodStatus(1);
            //添加反攻出库单号
            pickingCompleteDetail.setWodDocNum(sn);

            //添加物料id
            pickingCompleteDetail.setProductId(pickingCompleteDetailVo.getProductId());
            //添加物料料号
            pickingCompleteDetail.setItemCode(pickingCompleteDetailVo.getItemCode());

            //添加物料类别
            pickingCompleteDetail.setCategory(pickingCompleteDetailVo.getCategory());

            //添加净重
            pickingCompleteDetail.setNetWeight(pickingCompleteDetailVo.getNetWeight());
            //添加物料数量
            pickingCompleteDetail.setItemNum(pickingCompleteDetailVo.getItemNum());

            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetailVo.getPdoductNumber()));

            if (wmsStockInfo!=null){
                //毛重
                pickingCompleteDetail.setGrossWeight(wmsStockInfo.getWsiGorssWeight());
                //厚度
                pickingCompleteDetail.setThick(wmsStockInfo.getWsiThick());
                //幅宽
                pickingCompleteDetail.setBreadth(wmsStockInfo.getWsiWidth());
                //卷长
                pickingCompleteDetail.setRollLength(wmsStockInfo.getWsiLength());

                //仓储信息
                pickingCompleteDetail.setWarehouseInfo(wmsStockInfo.getWsiWarehourseInfo());
            }


            insert = pickingCompleteDetailMapper.insert(pickingCompleteDetail);
        }

        //新增返工出库单信息
        if (insert>0){
        PickingComplete pickingComplete=new PickingComplete();
        BeanUtils.copyProperties(pickingComPleteVo, pickingComplete);
        pickingComplete.setCreateUser(SecurityUtils.getUsername())
                .setCreateTime(new Date())
                .setDocumentType(DocumentTypeEnum.PICKING_COMPLETE.getCode())
                .setWodDocNum(sn)
                .setDeptId(SecurityUtils.getDeptId())
                .setWodStatus(1)
                .setPlannedWeight(list.size())
                .setMaterialId(productId)
                .setMaterialCode(materialCode)
                .setGenerationMode(GenerationMethodEnum.MANUALLY_GENERATED.getCode());
        return save(pickingComplete);
    }else {
            throw new GlobalException("新增失败");
        }

    }

    /**
     * 关结反攻出库单
     * @param pickingComPleteVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean closeComplete(PickingCompleteVo pickingComPleteVo) {
        //查询当前单据的状态，只能对待出库，待交接，状态的单据进行关结
        PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingComPleteVo.getId());
        Integer wodStatus = pickingComplete.getWodStatus();

        if (wodStatus==3||wodStatus==4){
            throw new GlobalException("不可对已关结和已完成状态的单据进行重复关结");
        }


        try {
            //如果单据状态是待检验，入库的物料显示在库，关联品质检验单状态更新为撤销
            if(wodStatus==5||wodStatus==6||wodStatus==2){
                //根据单号获取生产领料单明细
                List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()));

                ArrayList<WmsStockInfo> wmsStockInfos = new ArrayList<>();

                for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList) {
                    WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetail.getPdoductNumber()).eq(WmsStockInfo::getWsiMaterialStatus,5));
                    wmsStockInfos.add(wmsStockInfo);
                }

                if (wmsStockInfos.size()!=0){
                    boolean b = wmsStockInfoService.removeBatchByIds(wmsStockInfos);
                }

                if (wodStatus==5){
                    //如果是待检验修改品质检验单
                    UpdateQualityStatusVo updateQualityStatusVo = new UpdateQualityStatusVo();
                    updateQualityStatusVo.setStatus(8);
                    updateQualityStatusVo.setAssociatedNo(pickingComplete.getWodDocNum());
                    AjaxResult ajaxResult = qualityFeginService.updateQualityInspectionDocument(updateQualityStatusVo);


                    if (!"200".equals(ajaxResult.get("code").toString())){
                        throw new GlobalException("服务调用失败");
                    }

                }
            }
        }catch (Exception e){
            throw new GlobalException("关结失败");
        }


        //取出关结人
        String closeMan = pickingComplete.getCloseMan();
        if (closeMan!=null){
            closeMan=closeMan+","+SecurityUtils.getUsername();
        }else{
            closeMan=SecurityUtils.getUsername();
        }



        //修改单据状态未关结，修改关结原因，修改关结人，修改关结时间
        pickingComplete.setId(pickingComPleteVo.getId())
                .setWodStatus(4).setCloseTime(new Date())
                .setCloseMan(closeMan)
                .setCloseCause(pickingComPleteVo.getCloseCause());
        return updateById(pickingComplete);
    }

    /**
     * 修改反攻出库单
     * @param pickingComPleteVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateComplete(PickingCompleteVo pickingComPleteVo) {
        //判断明细是否至少有一条明细
        List<PickingCompleteDetailVo> list = pickingComPleteVo.getList();
        if (list.size()==0){
            throw new GlobalException("请至少添加一条物料明细");
        }    //通过物料id查出出库单号
        PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingComPleteVo.getId());
        int wodStatus = pickingComplete.getWodStatus();
        if (wodStatus!=1&&wodStatus!=4){
            throw new GlobalException("只能修改状态未”待提交“，”未通过“的单据");
        }
        //修改收发类型和出库日期和备注，文件路径
        pickingComplete.setTransceiverType(pickingComPleteVo.getTransceiverType());
        pickingComplete.setDateDelivery(pickingComPleteVo.getDateDelivery());
        pickingComplete.setOutOfStock(pickingComPleteVo.getOutOfStock());
        pickingComplete.setRemark(pickingComPleteVo.getRemark());

        //查出出库单号
        String wodDocNum = pickingComplete.getWodDocNum();
        //删除此出库单号对应的所有明细
        int delete = pickingCompleteDetailMapper.delete(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, wodDocNum));
        if (delete>0){
            int insert=0;

            //新增出库明细
            for (PickingCompleteDetailVo pickingCompleteDetailVo : list) {

                //新增返工出库单明细
                PickingCompleteDetail pickingCompleteDetail = new PickingCompleteDetail();
                //添加批次号
                pickingCompleteDetail.setBatchNumber(pickingCompleteDetailVo.getBatchNumber());
                //添加产品号码
                pickingCompleteDetail.setPdoductNumber(pickingCompleteDetailVo.getPdoductNumber());
                //添加物料id
                pickingCompleteDetail.setProductId(pickingCompleteDetailVo.getProductId());
                //添加物料编码
                pickingCompleteDetail.setItemCode(pickingCompleteDetailVo.getItemCode());
                //添加物料类别
                pickingCompleteDetail.setCategory(pickingCompleteDetailVo.getCategory());
                //添加状态为待入库
                pickingCompleteDetail.setWodStatus(1);
                //添加委外退货单号
                pickingCompleteDetail.setWodDocNum(wodDocNum);

                //添加净重
                pickingCompleteDetail.setNetWeight(pickingCompleteDetailVo.getNetWeight());
                //添加物料数量
                pickingCompleteDetail.setItemNum(pickingCompleteDetailVo.getItemNum());

                //调用feign查询毛重，净重，配比，克重，厚度，幅宽，卷厂，网型，铺网类型，
                insert=pickingCompleteDetailMapper.insert(pickingCompleteDetail);
            }
            pickingComplete.setPlannedWeight(list.size());
            updateById(pickingComplete);
            return insert;
        }else {
            throw new GlobalException("修改失败");
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteOutComplete(String ids) {
        //将ids根据逗号拆分成数组
        List<String> idStr = Arrays.asList(ids.split(","));
        if (idStr.size()==0){
            throw new GlobalException("请至少勾选一条单据");
        }
        //根据ids批量查询出list列表
        List<PickingComplete> pickingCompletes = pickingCompleteMapper.selectBatchIds(idStr);

        int delete=0;
        //遍历
        for (PickingComplete pickingComplete : pickingCompletes) {
            if (pickingComplete.getWodStatus()!=1){
                throw new GlobalException("只能删除“待出库”状态的单据");
            }else if(pickingComplete.getGenerationMode()!=2){
                throw new GlobalException("系统自动生成的单据不可删除");
            }
            delete = pickingCompleteDetailMapper.delete(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()));
        }
           if (delete>0){
               return  removeBatchByIds(idStr);
           }else{
               throw new GlobalException("删除失败");
           }

    }

    /**
     * 查看
     * @param id
     * @return
     */
    @Override
    public PickingCompleteDto view(Long id) {
        //根据字典查询收发类型
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_complete_type");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //查询出库信息
        PickingCompleteDto pickingCompleteDto = pickingCompleteMapper.getpickingComPleteById(id);


        //赋值收发类型
        pickingCompleteDto.setTransceiverTypeDict(statusMap.get(pickingCompleteDto.getTransceiverType()+""));
        pickingCompleteDto.setTransceiverType(pickingCompleteDto.getTransceiverType()+"");

        //获取单据类型

        pickingCompleteDto.setDocumentTypeDict(DocumentTypeEnum.getValue(Integer.parseInt(pickingCompleteDto.getDocumentType())));
        pickingCompleteDto.setDocumentType(pickingCompleteDto.getDocumentType());

        Warehouse warehouse = warehouseMapper.selectById(pickingCompleteDto.getOutOfStock());
        if(warehouse!=null){
            //添加仓库名称
            pickingCompleteDto.setOutOfStockDict(warehouse.getWarehouseName());
            pickingCompleteDto.setOutOfStock(pickingCompleteDto.getOutOfStock());
        }


        /*
            获取出库明细
         */
        List<PickingCompleteDetail> pickingCompleteDetails = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingCompleteDto.getWodDocNum()));

        List<PickingCompleteDetailDto> list = new ArrayList<PickingCompleteDetailDto>();
        for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetails) {

            //添加产品名称，产品规格
            PickingCompleteDetailDto pp = new PickingCompleteDetailDto();
            pp.setItemName(pickingCompleteDto.getItemName());
            pp.setItemSpec(pickingCompleteDto.getItemSpec());
            //添加产品号码
            pp.setPdoductNumber(pickingCompleteDetail.getPdoductNumber());
            //添加物料编码
            pp.setItemCode(pickingCompleteDetail.getItemCode());
            //添加净重
            pp.setNetWeight(pickingCompleteDetail.getNetWeight());
            //添加物料数量
            pp.setItemNum(pickingCompleteDetail.getItemNum());
            //添加批次号
            pp.setBatchNumber(pickingCompleteDetail.getBatchNumber());

            list.add(pp);

        }
        pickingCompleteDto.setList(list);
        return pickingCompleteDto;
    }

    /**
     * 返工出库列表
     * @param pickingComPleteVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<PickingCompleteInfoDto> getList(PickingCompleteVo pickingComPleteVo) {
        //查询返工出库列表
        List<PickingCompleteInfoDto> pickingCompletesList = pickingCompleteMapper.getpickingComPleteList(pickingComPleteVo);

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_complete_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //根据字典查出收发类型
        List<SysDictData> receivType = DictUtils.getDictCache("picking_complete_type");
        Map<String, String> typeMap = receivType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //新建一个dto集合
        List dtoList=new ArrayList();
        for (PickingCompleteInfoDto pickingCompleteInfoDto : pickingCompletesList) {

            //查看仓库名称
            Warehouse warehouse = warehouseMapper.selectById(pickingCompleteInfoDto.getOutOfStock());
            if (warehouse!=null){
                //添加仓库名称
                pickingCompleteInfoDto.setOutOfStock(warehouse.getWarehouseName());
                pickingCompleteInfoDto.setOutOfStockDict(pickingCompleteInfoDto.getOutOfStock()+"");
            }

            //添加单据类型
            pickingCompleteInfoDto.setDocumentType(DocumentTypeEnum.getValue(Integer.parseInt(pickingCompleteInfoDto.getDocumentType())));

            //添加收发类型
            pickingCompleteInfoDto.setTransceiverTypeDict(pickingCompleteInfoDto.getTransceiverType()+"");
            pickingCompleteInfoDto.setTransceiverType(typeMap.get(pickingCompleteInfoDto.getTransceiverType()+""));

            //添加单据状态
            pickingCompleteInfoDto.setWodStatusDict(pickingCompleteInfoDto.getWodStatus()+"");
            pickingCompleteInfoDto.setWodStatus(statusMap.get(pickingCompleteInfoDto.getWodStatus()+""));

            //添加生产方式
            pickingCompleteInfoDto.setGenerationMode(GenerationMethodEnum.getValue(Integer.parseInt(pickingCompleteInfoDto.getGenerationMode())));

            dtoList.add(pickingCompleteInfoDto);

        }
        return dtoList;
    }

    @DataScope(deptAlias = "t1")
    public List<GetPdaDto> getListPda(PickingCompleteVo pickingComPleteVo) {
        //查询返工出库列表
        List<GetPdaDto> list = pickingCompleteMapper.getpickingComPleteListPda(pickingComPleteVo);

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_complete_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (GetPdaDto getPdaDto : list) {
            getPdaDto.setWodStatusDict(statusMap.get(getPdaDto.getWodStatus()));
        }

        return list;
    }

    /**
     * 导出
     * @param pickingComPleteVo
     * @return
     */
    @Override
    public List<PickingCompleteExport> getInfoExport(PickingCompleteVo pickingComPleteVo) {

        if (pickingComPleteVo.getIdStr()!=null) {

            List<String> idList = Arrays.asList(pickingComPleteVo.getIdStr().split(","));
            pickingComPleteVo.setIds(idList);
        }

        List<PickingCompleteExport> list = pickingCompleteMapper.getInfoExport(pickingComPleteVo);

        //根据字典查出收发类型
        List<SysDictData> receivType = DictUtils.getDictCache("picking_complete_type");
        Map<String, String> typeMap = receivType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        // 网型
        List<SysDictData> meshTypeArray = DictUtils.getDictCache("sys_of_type");
        Map<String, String> meshTypeMap = meshTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        // 铺网类型
        List<SysDictData> layingTypeArray = DictUtils.getDictCache("sys_type_net");
        Map<String, String> layingTypeMap = layingTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (PickingCompleteExport pickingCompleteExport : list) {
            pickingCompleteExport.setTransceiverTypeDict(typeMap.get(pickingCompleteExport.getTransceiverTypeDict()));
            pickingCompleteExport.setMaterialNetDict(meshTypeMap.get(pickingCompleteExport.getMaterialNetDict()));
            pickingCompleteExport.setMaterialNetTypeDict(layingTypeMap.get(pickingCompleteExport.getMaterialNetTypeDict()));
        }
        
        return list;
    }

    /**
     * Pda出库
     * @param pickingRestDbVo
     * @return
     */
    @Override
    public PickingRestDbDto out(PickingRestDbVo pickingRestDbVo) throws Exception {
        //出库类型
        String type = pickingRestDbVo.getType();
        //输入值
        String inputValue = pickingRestDbVo.getInputValue();

        PickingRestDbDto pickingRestDbDto = new PickingRestDbDto();

        PickingComplete pickingComplete11 = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

        PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingComplete11.getMaterialCode()));

        //判断是否超收或者超发
        //获取菜单id
        Long menuId = feignService.getIdByMenuName("返工出库");
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);

        //如果配置了送检,则校验是否配了检验方案
        if (sysDocument.getDocInspectionMark()==1){

            List<SysDocumentCheckDto> documentCheckDtoList = sysDocument.getDocumentCheckDtoList();
            if (documentCheckDtoList==null){
                throw new GlobalException("请先在单据配置中配置送检方案");
            }

            PickingComplete byId = getById(pickingRestDbVo.getId());

            QualitySchemeConfigurationVo qualitySchemeConfigurationVo = new QualitySchemeConfigurationVo();
            qualitySchemeConfigurationVo.setCategory(publicGoods.getMaterialCategory());
            qualitySchemeConfigurationVo.setItemRank(documentCheckDtoList.get(0).getCheckSubmit());
            qualitySchemeConfigurationVo.setMaterialCategory(publicGoods.getMaterialType());
            qualitySchemeConfigurationVo.setMaterialCode(byId.getMaterialCode());
            boolean check = qualityFeginService.check(qualitySchemeConfigurationVo);

            if (!check){
                throw new GlobalException("该物料不存在对应的检验方案");

            }
        }

        //如果是扫码出库
        if ("1".equals(type)) {
            //判断扫描的物料条码是否一致
            PickingCompleteDetail pickingCompleteDetail = pickingCompleteDetailMapper.selectOne(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getPdoductNumber, inputValue).eq(PickingCompleteDetail::getWodStatus,1).eq(PickingCompleteDetail::getWodDocNum,pickingComplete11.getWodDocNum()));
            if (pickingCompleteDetail == null) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }
            //扫描的物料编码
            String wsiMaterialCode = pickingCompleteDetail.getItemCode();
            //查询委外发货单中的物料编码、
            PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

            if (!pickingComplete.getMaterialCode().equals(wsiMaterialCode)) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }
            //修改明细状态为已出库
            pickingCompleteDetail.setWodStatus(4);
            //判断是否有检验
            int docInspectionMark = sysDocument.getDocInspectionMark();
            //如果有检验
            if (docInspectionMark==1){
                //修改单据明细为待检验
                pickingCompleteDetail.setWodStatus(2);
            }else{
                //判断是否有交接
                int docHandoverMark = sysDocument.getDocHandoverMark();
                //有交接
                if (docHandoverMark==1){
                    //修改单据明细为待交接
                    pickingCompleteDetail.setWodStatus(3);

                }
            }
            //修改明细状态
            pickingCompleteDetailMapper.updateById(pickingCompleteDetail);

            //修改返工出库单的单据状态为出库中
            pickingComplete.setWodStatus(2);
            //修改出库重量和出库数量
            Double outgoingWeight = pickingComplete.getOutgoingWeight();
            if (outgoingWeight==null){
                outgoingWeight=0.0;
            }
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(outgoingWeight));
            BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(pickingCompleteDetail.getNetWeight()));

            BigDecimal add = bigDecimal.add(bigDecimal1);
            pickingComplete.setOutgoingWeight(add.doubleValue());
            Integer outgoingNumber = pickingComplete.getOutgoingNumber();
            if (outgoingNumber==null){
                outgoingNumber=0;
            }
            pickingComplete.setOutgoingNumber(outgoingNumber+1);
            updateById(pickingComplete);

            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue));
            //修改仓库物料的状态为备料
            wmsStockInfo.setWsiMaterialStatus(5);
            int i = wmsStockInfoMapper.updateById(wmsStockInfo);

            //计划/实际
            pickingRestDbDto.setQuantityIdentification(pickingComplete.getPlannedWeight()+"/"+pickingComplete.getOutgoingNumber());
            //出库数量
            pickingRestDbDto.setAllotNumOut(pickingComplete.getOutgoingNumber()+"");

            pickingRestDbDto.setMessage("物料号码:" + inputValue + ",净重:" + wmsStockInfo.getWsiNetWeight() + ",数量:1" + "录入成功");

            OutboundLog outboundLog = new OutboundLog();
            BeanUtils.copyProperties(wmsStockInfo,outboundLog,new String[]{"id"});
            outboundLogService.save(outboundLog);

            //拣货方案
            SchemeDto point = point(pickingRestDbVo.getId());


            if (point.getCombinationName()!=null){
                pickingRestDbDto.setPickingPrompt(point.getCombinationName());
            }


            //查询当前生产领料单
            PickingComplete pickingComplete1 = pickingCompleteMapper.selectById(pickingRestDbVo.getId());
            //判断是否有超收
            int docHairMark = sysDocument.getDocHairMark();
            //没有超收
            if (docHairMark!=1 && publicGoods.getMaterialHair()!=1){

                    //计划出库数量
                    int allotAmount = pickingComplete1.getPlannedWeight();
                    //实际出库数量
                    int allotWeightOut = pickingComplete1.getOutgoingNumber();
                    if (allotWeightOut >= allotAmount) {
                        //判断是否有检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark==1){
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingComplete.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingComplete.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingCompleteDetail> stockOutInfoDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()));

                            String batchNum="";

                            for (int j=0;j<stockOutInfoDetailList.size();j++){
                                if (j==0){
                                    batchNum=stockOutInfoDetailList.get(j).getBatchNumber();
                                }else{
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getBatchNumber())){
                                        batchNum= batchNum+","+stockOutInfoDetailList.get(j).getBatchNumber();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getPdoductNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getBatchNumber());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingComplete1.setWodStatus(5);
                            pickingCompleteMapper.updateById(pickingComplete1);
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark==1){
                            //修改单据为待交接
                            pickingComplete1.setWodStatus(6);
                            pickingCompleteMapper.updateById(pickingComplete1);

                        }else{
                            //修改单据为已完成
                            pickingComplete1.setWodStatus(3);
                            pickingCompleteMapper.updateById(pickingComplete1);
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber,inputValue).eq(WmsStockInfo::getWsiMaterialStatus,5));
                        }
                    }
            }


        }else if ("2".equals(type)){

            PickingComplete pickingComplete2 = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

            //批次出库
            //查询当前物料明细中状态为待出库的物料
            List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getBatchNumber, inputValue).eq(PickingCompleteDetail::getWodStatus,1).eq(PickingCompleteDetail::getWodDocNum,pickingComplete11.getWodDocNum()));

            if (pickingCompleteDetailList.size()==0){
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }

            //查询当前批次(明细表中物料状态为待出库的物料净重之和)
            Double netWeight = pickingCompleteDetailMapper.getNetWeight(inputValue,pickingComplete2.getWodDocNum());

            //查询委外出库单中的物料编码
            PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

            List<PickingCompleteDetail> list = new ArrayList<>();
            List<WmsStockInfo> list1 = new ArrayList<WmsStockInfo>();

            List<OutboundLog> outboundLogList = new ArrayList<OutboundLog>();

            for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList) {
                //修改明细状态为已出库
                pickingCompleteDetail.setWodStatus(4);
                //判断是否有检验
                int docInspectionMark = sysDocument.getDocInspectionMark();
                //如果有检验
                if (docInspectionMark==1){
                    //修改单据明细为待检验
                    pickingCompleteDetail.setWodStatus(2);
                }else{
                    //判断是否有交接
                    int docHandoverMark = sysDocument.getDocHandoverMark();
                    //有交接
                    if (docHandoverMark==1){
                        //修改单据明细为待交接
                        pickingCompleteDetail.setWodStatus(3);

                    }
                }
                //修改明细状态
                list.add(pickingCompleteDetail);


                //修改出库重量和出库数量
                Double outgoingWeight = pickingComplete.getOutgoingWeight();
                if (outgoingWeight==null){
                    outgoingWeight=0.0;
                }
                BigDecimal bigDecimal = new BigDecimal(outgoingWeight);
                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(pickingCompleteDetail.getNetWeight()));

                BigDecimal add = bigDecimal.add(bigDecimal1);
                pickingComplete.setOutgoingWeight(add.doubleValue());

                Integer outgoingNumber = pickingComplete.getOutgoingNumber();
                if (outgoingNumber==null){
                    outgoingNumber=0;
                }
                pickingComplete.setOutgoingNumber(outgoingNumber+1);


                //修改仓库物料的状态为备料
                WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetail.getPdoductNumber()));
                list1.add(wmsStockInfo);
                wmsStockInfo.setWsiMaterialStatus(5);

                OutboundLog outboundLog = new OutboundLog();
                BeanUtils.copyProperties(wmsStockInfo,outboundLog,new String[]{"id"});
                outboundLogList.add(outboundLog);
            }

            pickingCompleteDetailService.updateBatchById(list);
            wmsStockInfoService.updateBatchById(list1);

            //修改返工出库单的单据状态为出库中
            pickingComplete.setWodStatus(2);
            updateById(pickingComplete);

            PickingComplete pickingComplete1 = pickingCompleteMapper.selectById(pickingRestDbVo.getId());
            //返回前端参数
            //计划/实际
            pickingRestDbDto.setQuantityIdentification(pickingComplete1.getPlannedWeight()+"/"+pickingComplete1.getOutgoingNumber());
            //出库数量
            pickingRestDbDto.setAllotNumOut(pickingComplete1.getOutgoingNumber()+"");


            pickingRestDbDto.setMessage("批次号:" + inputValue + ",总净重:" + netWeight + ",数量:"+pickingCompleteDetailList.size() + "录入成功");

            outboundLogService.saveBatch(outboundLogList);

            //拣货方案
            SchemeDto point = point(pickingRestDbVo.getId());


            if (point.getCombinationName()!=null){
                pickingRestDbDto.setPickingPrompt(point.getCombinationName());
            }


            //判断是否有超收
            int docHairMark = sysDocument.getDocHairMark();
            //没有超收
            if (docHairMark!=1 && publicGoods.getMaterialHair()!=1){

                //计划出库数量
                int allotAmount = pickingComplete1.getPlannedWeight();
                //实际出库数量
                int allotWeightOut = pickingComplete1.getOutgoingNumber();
                    if (allotWeightOut >= allotAmount) {
                        //判断是否有检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark==1){
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingComplete.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingComplete.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingCompleteDetail> stockOutInfoDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()));

                            String batchNum="";

                            for (int j=0;j<stockOutInfoDetailList.size();j++){
                                if (j==0){
                                    batchNum=stockOutInfoDetailList.get(j).getBatchNumber();
                                }else{
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getBatchNumber())){
                                        batchNum= batchNum+","+stockOutInfoDetailList.get(j).getBatchNumber();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getPdoductNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getBatchNumber());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingComplete1.setWodStatus(5);
                            pickingCompleteMapper.updateById(pickingComplete1);
                            pickingRestDbDto.setCode("200");
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark==1){
                            //修改单据为待交接
                            pickingComplete1.setWodStatus(6);
                            pickingCompleteMapper.updateById(pickingComplete1);

                        }else{
                            //修改单据为已完成
                            pickingComplete1.setWodStatus(3);
                            pickingCompleteMapper.updateById(pickingComplete1);
                            //根据批次删除库存
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialBatch,inputValue).eq(WmsStockInfo::getWsiMaterialStatus,5));
                        }
                    }
            }


        }else if ("3".equals(type)){
            //条码撤销
            PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

            if (pickingComplete.getWodStatus()!=1&&pickingComplete.getWodStatus()!=2){
                throw new GlobalException("只能对待出库和出库中的单据进行条码撤销");
            }
            //在明细表中查询是否有此条码
            PickingCompleteDetail pickingCompleteDetail = pickingCompleteDetailMapper.selectOne(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getPdoductNumber, inputValue).eq(PickingCompleteDetail::getWodDocNum,pickingComplete11.getWodDocNum()).eq(PickingCompleteDetail::getWodDocNum,pickingComplete11.getWodDocNum()));
            if (pickingCompleteDetail==null){
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("撤销失败");
                return pickingRestDbDto;
            }


            //修改库存状态为在库
            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue));
            wmsStockInfo.setWsiMaterialStatus(2);
            wmsStockInfoMapper.updateById(wmsStockInfo);


            OutboundLog outboundLog = outboundLogMapper.selectOne(new QueryWrapper<OutboundLog>().lambda().eq(OutboundLog::getWsiMaterialNumber, inputValue));


            //删除日志
            outboundLogService.removeById(outboundLog);


            //修改出库明细状态为待出库
            pickingCompleteDetail.setWodStatus(1);
            pickingCompleteDetailMapper.updateById(pickingCompleteDetail);


            BigDecimal out = new BigDecimal(String.valueOf(pickingComplete.getOutgoingWeight()));
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight()));
            BigDecimal outWeight = out.subtract(bigDecimal);
            //修改出库重量
            pickingComplete.setOutgoingWeight(outWeight.doubleValue());

            //扣除数量
            int num = pickingComplete.getOutgoingNumber();
            pickingComplete.setOutgoingNumber(num - 1);


            int i = pickingCompleteMapper.updateById(pickingComplete);
            if (i <= 0) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("撤销失败");
            }

            //删除品质检验单
            pickingRestDbDto.setMessage("撤销成功");
        }
        pickingRestDbDto.setCode("200");
        return pickingRestDbDto;
    }

    /**
     * 拣货完成
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean finishProduction(Long id) throws Exception {
        //判断当前页面是否有配置送检和交接，如果有则生成品检单

        //获取菜单id
        Long menuId = feignService.getIdByMenuName("返工出库");
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = com.alibaba.fastjson2.JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);

        //查出生产领料信息单
        PickingComplete pickingComplete = pickingCompleteMapper.selectById(id);

        //判断是否有检验
        int docInspectionMark = sysDocument.getDocInspectionMark();
        //如果有检验
        if (docInspectionMark==1){
            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingComplete.getMaterialCode()));
            //生成品质检验单
            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
            //添加类别，编码，物料类别
            qualityInspectionDocumentVo.setMaterialCode(pickingComplete.getMaterialCode());
            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
            qualityInspectionDocumentVo.setAssociatedNo(pickingComplete.getWodDocNum());
            qualityInspectionDocumentVo.setModeType(1);
            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
            //查询当前明细表中的所有物料号码
            List<PickingCompleteDetail> stockOutInfoDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()));

            String batchNum="";

            for (int j=0;j<stockOutInfoDetailList.size();j++){
                if (j==0){
                    batchNum=stockOutInfoDetailList.get(j).getBatchNumber();
                }else{
                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getBatchNumber())){
                        batchNum= batchNum+","+stockOutInfoDetailList.get(j).getBatchNumber();
                    }
                }

                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getPdoductNumber());
                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getBatchNumber());
                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
            }
            qualityInspectionDocumentVo.setBatchNo(batchNum);

            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
            //修改单据状态为待检验
            pickingComplete.setWodStatus(5);
            return pickingCompleteMapper.updateById(pickingComplete)>0;
        }

        //判断是否有交接
        int docHandoverMark = sysDocument.getDocHandoverMark();
        //有交接
        if (docHandoverMark==1){
            //修改单据状态为待交接
            pickingComplete.setWodStatus(6);
            return pickingCompleteMapper.updateById(pickingComplete)>0;
        }

        //判断是否有超收
        int docHairMark = sysDocument.getDocHairMark();

        //判断明细中是否还有单据未扫
        List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingComplete.getWodDocNum()).eq(PickingCompleteDetail::getWodStatus, 1));

        if (pickingCompleteDetailList.size()>0){
            throw new GlobalException("出库明细中还有待出库单据未扫");
        }

        //删除库存
        List list = new ArrayList<WmsStockInfo>();
        for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList) {
            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetail.getPdoductNumber()));
            list.add(wmsStockInfo);
        }
        wmsStockInfoMapper.deleteBatchIds(list);

        //修改单据状态为已完成
        pickingComplete.setWodStatus(3);
        return pickingCompleteMapper.updateById(pickingComplete)>0;
    }



    //交接
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickingRestHandoverDto overProduction(PickingRestDbVo pickingRestDbVo) {
        //获取步骤
        int step = pickingRestDbVo.getExecutionOrder();
        //获取输入值
        String inputValue = pickingRestDbVo.getInputValue();
        //返回值
        PickingRestHandoverDto pickingRestHandoverDto = new PickingRestHandoverDto();
        //获取菜单id
        Long menuId = feignService.getIdByMenuName("返工出库");
        //交接方式
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
        //拿到交接方式
        int jjType = sysDocument.getDocHandoverMethod();

        //根据字典查出单位
        List<SysDictData> unitList = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));



        if (step==1) {
            //校验员工编号
            AjaxResult byUserName = remoteUserService.getByUserName(pickingRestDbVo.getInputValue());
            SysUser userInfo = JSON.parseObject(JSON.toJSONString(byUserName.get("data")), SysUser.class);

            if (userInfo.getUserName() == null) {
                pickingRestHandoverDto.setResultCode("500");
                pickingRestHandoverDto.setMessage("交接人输入有误,请核对后输入");
                return pickingRestHandoverDto;
            }

            //返回交接人
            pickingRestHandoverDto.setHandoverName(userInfo.getUserName() + "-" + userInfo.getNickName());
            pickingRestHandoverDto.setDocHandoverMethod(jjType);



            if (jjType == 1) {
                pickingRestHandoverDto.setMessage("请输入物料条码");
            } else if (jjType == 2) {
                pickingRestHandoverDto.setMessage("请输入批次号");
            } else if (jjType == 3) {
                PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

                pickingRestDbVo.setInputValue(pickingComplete.getWodDocNum());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingComplete.getMaterialCode()));

                //判断是否存在明细表中
                List<PickingCompleteDetail> pickingCompleteDetailList1 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingRestDbVo.getInputValue()));
                if (pickingCompleteDetailList1 == null) {
                    pickingRestHandoverDto.setMessage("交接失败");
                    pickingRestHandoverDto.setResultCode("500");
                    return pickingRestHandoverDto;
                }

                //查询当前单据号在明细中是否存在，且状态为待交接
                List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, pickingRestDbVo.getInputValue()).eq(PickingCompleteDetail::getWodStatus, 3));
                if (pickingCompleteDetailList.size() == 0) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接失败,请核实后再录入");
                    return pickingRestHandoverDto;
                } else {
                    //查出当前为待交接状态的单据明细,算出总净重和总数量
                    Double sumPiHao = pickingCompleteDetailMapper.getDoc(pickingRestDbVo.getInputValue());
                    //总数量
                    long sumNum = pickingCompleteDetailList.size();

                    //出库重量
                    Double allotAmount = pickingComplete.getOutgoingWeight();

                    //返回计划/实际
                    Double overWeight = pickingComplete.getOverWeight();
                    if (overWeight==null){
                        overWeight=0.0;
                    }
                    //(实际交接)
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(sumPiHao));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingComplete.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));


                    //保存交接数量，交接重量
                    Integer overNum = pickingComplete.getOverNum();
                    if (overNum==null){
                        overNum=0;
                    }
                    int jjSum = (int) sumNum;
                    pickingComplete.setOverNum(overNum + jjSum);

                    //取出关节人
                    String closeUser = pickingComplete.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingComplete.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingComplete.setCloseMan("," + name);
                    }
                    pickingComplete.setCloseTime(new Date());


                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingComplete.getOutgoingNumber() + "/" + pickingComplete.getOverNum());

                    for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList) {
                        //修改物料状态明细为已出库
                        pickingCompleteDetail.setWodStatus(4);
                        pickingCompleteDetailMapper.updateById(pickingCompleteDetail);
                    }


                    //判断是否所有物料没有待交接的状态
                    List<PickingCompleteDetail> pickingCompleteDetailList2 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodStatus, 3).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));

                    if (pickingCompleteDetailList2.size() == 0) {
                        //修改单据状态为已完成
                        pickingComplete.setWodStatus(3);
                        //删除库存
                        List list = new ArrayList<WmsStockInfo>();

                        List<PickingCompleteDetail> pickingCompleteDetailList3 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));

                        for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList3) {
                            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetail.getPdoductNumber()));
                            list.add(wmsStockInfo);
                        }
                        wmsStockInfoMapper.deleteBatchIds(list);

                    }
                    boolean b = updateById(pickingComplete);
                    if (!b) {
                        pickingRestHandoverDto.setMessage("交接失败");
                        pickingRestHandoverDto.setResultCode("500");
                        return pickingRestHandoverDto;
                    }

                    //消息提示
                    pickingRestHandoverDto.setMessage("单据号:" + pickingRestDbVo.getInputValue() + ",总净重" + sumPiHao + "数量:" + sumNum + ",交接成功");
                }
            }
            pickingRestHandoverDto.setResultCode("200");
            return pickingRestHandoverDto;
        }else if (step==2) {
            //如果是单个物料交接
            if (jjType == 1) {
                //查询当前的出库单信息
                PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingComplete.getMaterialCode()));


                //查询出库明细
                PickingCompleteDetail pickingCompleteDetail = pickingCompleteDetailMapper.selectOne(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getPdoductNumber, inputValue).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));
                if (pickingCompleteDetail == null) {
                    pickingRestHandoverDto.setMessage("交接失败");
                    pickingRestHandoverDto.setMessage("500");
                    return pickingRestHandoverDto;
                }

                //判断当前物料是否属于待交接
                if (3!=pickingCompleteDetail.getWodStatus()) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("不可重复交接");
                    return pickingRestHandoverDto;
                } else {
                    //出库重量
                    Double allotAmount = pickingComplete.getOutgoingWeight();

                    Double overWeight = pickingComplete.getOverWeight();
                    if (overWeight == null) {
                        overWeight = 0.0;
                    }

                    //(实际交接)
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(pickingCompleteDetail.getNetWeight()));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingComplete.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));

                    Integer overNum = pickingComplete.getOverNum();
                    if (overNum == null) {
                        overNum = 0;
                    }
                    //保存交接数量，交接重量
                    pickingComplete.setOverNum(overNum + 1);

                    //取出关节人
                    String closeUser = pickingComplete.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingComplete.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingComplete.setCloseMan("," + name);
                    }
                    pickingComplete.setCloseTime(new Date());
                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingComplete.getOutgoingNumber() + "/" + pickingComplete.getOverNum());

                    //修改物料状态明细为已出库
                    pickingCompleteDetail.setWodStatus(4);
                    pickingCompleteDetailMapper.updateById(pickingCompleteDetail);


                    //判断是否所有物料没有待交接的状态
                    List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodStatus, 3).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));
                    if (pickingCompleteDetailList.size() == 0) {
                        //修改单据状态为已完成
                        pickingComplete.setWodStatus(3);

                    }

                    //删除库存
                    wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber,inputValue).eq(WmsStockInfo::getWsiMaterialStatus,5));
                    boolean b = updateById(pickingComplete);
                    if (!b) {
                        pickingRestHandoverDto.setMessage("交接失败");
                        pickingRestHandoverDto.setMessage("500");
                        return pickingRestHandoverDto;
                    }

                    //消息提示
                    pickingRestHandoverDto.setMessage("物料号码:" + pickingRestDbVo.getInputValue() + ",净重" + pickingCompleteDetail.getNetWeight() + "数量:1,交接成功");

                }
            } else if (jjType == 2) {
                PickingComplete pickingComplete = pickingCompleteMapper.selectById(pickingRestDbVo.getId());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingComplete.getMaterialCode()));

                //判断是否存在明细表中
                List<PickingCompleteDetail> pickingCompleteDetailList1 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getBatchNumber, pickingRestDbVo.getInputValue()).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));
                if (pickingCompleteDetailList1 == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接失败");
                    return pickingRestHandoverDto;
                }

                //查询当前批号在明细中是否存在，且状态为待交接
                List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getBatchNumber, pickingRestDbVo.getInputValue()).eq(PickingCompleteDetail::getWodStatus, 3).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));
                if (pickingCompleteDetailList.size() == 0) {
                    pickingRestHandoverDto.setMessage("500");
                    pickingRestHandoverDto.setMessage("不可重复交接");
                    return pickingRestHandoverDto;
                } else {
                    //查出当前为待交接状态的批次，算出总净重和总数量
                    Double sumPiHao = pickingCompleteDetailMapper.getPiHao(pickingRestDbVo.getInputValue());
                    //总数量
                    long sumNum = pickingCompleteDetailList.size();

                    //出库重量
                    Double allotAmount = pickingComplete.getOutgoingWeight();

                    //返回计划/实际
                    Double overWeight = pickingComplete.getOverWeight();
                    if (overWeight == null) {
                        overWeight = 0.0;
                    }
                    //(实际交接)
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(sumPiHao));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingComplete.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));


                    //保存交接数量，交接重量
                    Integer overNum = pickingComplete.getOverNum();
                    if (overNum == null) {
                        overNum = 0;
                    }

                    int jjSum = (int) sumNum;
                    pickingComplete.setOverNum(overNum + jjSum);

                    //取出关节人
                    String closeUser = pickingComplete.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingComplete.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingComplete.setCloseMan("," + name);
                    }
                    pickingComplete.setCloseTime(new Date());

                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingComplete.getOutgoingNumber() + "/" + pickingComplete.getOverNum());

                    for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList) {
                        //修改物料状态明细为已出库
                        pickingCompleteDetail.setWodStatus(4);
                    }
                    pickingCompleteDetailService.updateBatchById(pickingCompleteDetailList);

                    //判断是否所有物料没有待交接的状态
                    List<PickingCompleteDetail> pickingCompleteDetailList2 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodStatus, 3).eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));

                    if (pickingCompleteDetailList2.size() == 0) {
                        //修改单据状态为已完成
                        pickingComplete.setWodStatus(3);
                        //删除库存
                        List list = new ArrayList<WmsStockInfo>();

                        List<PickingCompleteDetail> pickingCompleteDetailList3 = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum,pickingComplete.getWodDocNum()));

                        for (PickingCompleteDetail pickingCompleteDetail : pickingCompleteDetailList3) {
                            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingCompleteDetail.getPdoductNumber()));
                            list.add(wmsStockInfo);
                        }
                        wmsStockInfoMapper.deleteBatchIds(list);
                    }
                    boolean b = updateById(pickingComplete);
                    if (!b) {
                        pickingRestHandoverDto.setResultCode("500");
                        pickingRestHandoverDto.setMessage("交接失败");
                        return pickingRestHandoverDto;
                    }

                    //消息提示
                    pickingRestHandoverDto.setMessage("批次号:" + pickingRestDbVo.getInputValue() + ",总净重" + sumPiHao + "数量:" + sumNum + ",交接成功");
                }
            } else if (jjType == 3) {
                pickingRestHandoverDto.setMessage("请勿重复交接");
                pickingRestHandoverDto.setResultCode("500");
            }
        }
        pickingRestHandoverDto.setResultCode("200");
        return pickingRestHandoverDto;
    }

    /**
     * 修改品质
     * @param updateDocVo
     * @return
     */
    @Override
    public boolean competeUpdateStatus(UpdateDocVo updateDocVo) {

            //获取菜单id
            Long menuId = feignService.getIdByMenuName("返工出库");
            //交接方式
            AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
            SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
            //交接标志
            int docHandoverMethod = sysDocument.getDocHandoverMark();

            //判断是否有交接


            //如果有交接，修改单据状态和物料明细状态为代交接
            if (docHandoverMethod==1){
                PickingComplete pickingComplete = pickingCompleteMapper.selectOne(new QueryWrapper<PickingComplete>().lambda().eq(PickingComplete::getWodDocNum, updateDocVo.getDocNum()));
                pickingComplete.setWodStatus(6);//待交接
                updateById(pickingComplete);
                List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, updateDocVo.getDocNum()));
                for (PickingCompleteDetail PickingCompleteDetail : pickingCompleteDetailList) {
                    PickingCompleteDetail.setWodStatus(3);
                }
                return pickingCompleteDetailService.updateBatchById(pickingCompleteDetailList);

            }else{
                PickingComplete pickingComplete = pickingCompleteMapper.selectOne(new QueryWrapper<PickingComplete>().lambda().eq(PickingComplete::getWodDocNum, updateDocVo.getDocNum()));
                pickingComplete.setWodStatus(3);//已完成
                updateById(pickingComplete);

                List<WmsStockInfo> wmsStockInfoList = new ArrayList<>();

                List<PickingCompleteDetail> pickingCompleteDetailList = pickingCompleteDetailMapper.selectList(new QueryWrapper<PickingCompleteDetail>().lambda().eq(PickingCompleteDetail::getWodDocNum, updateDocVo.getDocNum()));
                for (PickingCompleteDetail PickingCompleteDetail : pickingCompleteDetailList) {
                    PickingCompleteDetail.setWodStatus(4);

                    WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, PickingCompleteDetail.getPdoductNumber()));
                    wmsStockInfoList.add(wmsStockInfo);

                }
                wmsStockInfoService.removeBatchByIds(wmsStockInfoList);
                return  pickingCompleteDetailService.updateBatchById(pickingCompleteDetailList);
            }
        }

    /**
     * 拣货方案
     * @param id
     * @return
     */
    @Override
    public SchemeDto point(Long id) {

        PickingCompleteDetail pickingCompleteDetail = pickingCompleteDetailMapper.getKw(id);

        if (pickingCompleteDetail==null){
            return new SchemeDto();
        }

        String pdoductNumber = pickingCompleteDetail.getPdoductNumber();
        //通过产品编码查出库位id
        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pdoductNumber));

        Warehouse warehouse = warehouseService.getById(wmsStockInfo.getWsiWarehourse());
        SchemeDto schemeDto = new SchemeDto();
        if(warehouse != null){
            schemeDto.setCombinationName(warehouse.getWarehouseName());
        }
        return schemeDto;
    }


}
